﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using SweeperPrototype.Business.Fields;
using SweeperPrototype.Business.Keys;
using SweeperPrototype.Objects;

namespace SweeperPrototype.Business.Plumbing
{
    public interface IPositionSource
    {
        List<PositionSubscription> FetchPositions(BookCode code);
    }

    public class MockPositionSource : IPositionSource
    {
        public MockPositionSource( BookCode code )
        {
            Random r = new Random();

            for( int i=0 ; i < 50 ; i++ )
            {
                PositionSubscription sub = new PositionSubscription(code,
                    new ExposureRIC("RIC" + i.ToString()));
                sub.Update(
                    new PositionSubscription.PositionUpdate(
                    new Shares(r.Next(1000, 1000000)),
                    new Timestamp(DateTime.Now)));
                subscriptions.Add( sub );                
            }
        }

        List<PositionSubscription> subscriptions = new List<PositionSubscription>();

        public List<PositionSubscription> FetchPositions( BookCode code )
        {
            Random r = new Random();

            //Thread.Sleep( r.Next( 10, 500 ) );
            int count = subscriptions.Count;//r.Next(300, 300);
            for( int i=0 ; i < count ; i++ )
            {
                PositionSubscription sub = subscriptions[i];

                if( r.NextDouble() < .1 )
                {
                    int adj = (r.Next(0, 2) - 1) * 100;  
                    Shares shares = new Shares( sub.Quantity.Value + adj  );

                    sub.Update(
                        new PositionSubscription.PositionUpdate(
                        shares,
                        new Timestamp(DateTime.Now)));
                }

            }
            return subscriptions;
        }
    }
}
